import qs from 'qs'
export default {
    name: 'Notified',
    components: {

    },
    data() {
        return {
            handleCardDialog: false,
            notifiedData: [],
            height: '600',
            pagesize: 5,
            total: 0,
            currentPage: 1,
            searchForm: {
                deviceId: '',
                unread: false,
                alarmType: ""
            },
            alarmTypeData: [],
            isSelected: false,
            handleEmailDialog: false,
            formLabelWidth: "120px",
            emailForm: {
                email: "",
                remarks: "",
                faultId: "",
            },
            cardForm: {
                phone: "",
                remarks: ""
            },
            multipleSelection: [],
            unread: false,
            users:"",
            handleupadateDialog:false,
            upadateForm:{
                faultId:"",
                remarks:"",
            },
            formDisabled:false,
        }
    },
    computed: {
        offset: function () {
            return (this.currentPage - 1) * this.pagesize;
        }
    },
    watch: {
        handleEmailDialog: function (oldVal, newVal) {
            if (oldVal) {
                this.emailForm = {
                    email: "",
                    remarks: "",
                }
            }
        },
        handleCardDialog:function(oldVal,newVal){
            if(oldVal){
                this.cardForm = {
                    phone: "",
                    remarks: ""
                }
            }
        }
    },
    created() {
        this.initExceptinolist();
        this.users =JSON.parse(window.localStorage.getItem("userInfo"));
    },
    mounted() {

    },
    methods: {
        initExceptinolist() {
            var users = JSON.parse(window.localStorage.getItem("userInfo"));
            this.$http.get("/alarmRecord/search?userId=" + users.uId + "&limit=" + this.pagesize + "&offset=" + this.offset +
                "&deviceID=" + this.searchForm.deviceId + "&unread=" + this.unread + "&alarmType=" + this.searchForm.alarmType)
                .then((res) => {
                    if (res.status == 200) {
                        this.notifiedData = res.data;
                        this.total = res.total;
                    }
                }).catch((err) => {
                console.log(err);
            });
        },
        search() {
            this.currentPage = 1;
            this.initExceptinolist();
        },
        unreadFormatter(row, cloumn, cellValue, index) {
            return cellValue ? '未读' : '已读';
        },
        emailNotifiedClick() {
            if (this.multipleSelection.length == 0) {
                this.$message.error("至少选择一个设备信息");
                return false;
            } else if (this.multipleSelection.length > 1) {
                this.$message.error("只能选择一行数据进行操作");
                return false;
            } else {
                this.handleEmailDialog = true;
            }
        },
        //邮件发送
        createEmailSure() {
            var emailRule = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/; //邮箱正则
            if (!emailRule.test(this.emailForm.email)) {
                 this.$message.error("邮箱格式错误");
                 return false;
            } else {
                var postData = [];
                this.multipleSelection.forEach(function (val, idx) {
                    postData.push(val.faultId);
                });
                var faultId = postData.toString();
                var postData = {
                    email:this.emailForm.email,
                    remark:this.emailForm.remarks,
                    userId:this.users.uId,
                    faultId:faultId
                }
                this.$http.post('/alarmRecord/emailNotification',qs.stringify(postData))
                 .then((res) => {
                   if(res){
                     if(res.status == 200){
                        this.$message({
                            type:"success",
                            message:"邮箱发送成功"
                        });
                        this.initExceptinolist();
                        this.handleEmailDialog = false;
                        return false;
                     }
                   }
                   this.$message.error("接收失败");
                 }).catch((err) => {
                   console.log(err);
               });
            }
        },
        cardNotifiedClick(){
            if (this.multipleSelection.length == 0) {
                this.$message.error("至少选择一个设备信息");
                return false;
            } else if (this.multipleSelection.length > 1) {
                this.$message.error("只能选择一行数据进行操作");
                return false;
            } else {
                var postData = [];
                this.multipleSelection.forEach(function (val, idx) {
                    postData.push(val.faultId);
                });
                var faultId = postData.toString();
                this.cardForm.faultId = faultId;
                this.handleCardDialog = true;
            }
        },
        //短信发送
        createSureCard() {
            var telRule = /^1[0-9]{10}$/; //电话正则
            if (!telRule.test(this.cardForm.phone)) {
                this.$message.error("手机号码格式错误");
                return false;
            }else{
                var postData = [];
                this.multipleSelection.forEach(function (val, idx) {
                    postData.push(val.faultId);
                });
                var faultId = postData.toString();
                var postData = {
                    phone:this.cardForm.phone,
                    remark:this.cardForm.remarks,
                    userId:this.users.uId,
                    faultId:faultId
                }
                this.$http.post('/alarmRecord/shortNoteNotification',qs.stringify(postData))
                 .then((res) => {
                   if(res){
                     if(res.status == 200){
                        this.$message({
                             type:"success",
                             message:"短信发送成功"
                       });
                       this.initExceptinolist();
                       this.handleCardDialog = false;
                       return false;
                     }
                   }
                   this.$message.error("接收失败");
                 }).catch((err) => {
                console.log(err);
             });
           }
        },
        rowClick(row){
            this.upadateForm.faultId = row.faultId;
            this.upadateForm.remarks = row.remark;
            this.formDisabled = true;
            this.handleupadateDialog = true;
        },
        modifyRemarks(){
            var postData = {
                faultId :this.upadateForm.faultId,
                remark:this.upadateForm.remarks,
            }
            this.$http.post("/alarmRecord/updateRemark",qs.stringify(postData))
             .then((res) => {
               if(res.status == 200){
                   this.$message({
                       type:"success",
                       message:"修改成功"
                   });
                   this.initExceptinolist();
                   this.handleupadateDialog = false;
                   return;
                }
                this.$message.error("修改失败");
              }).catch((err) => {
               console.log(err);
            });
        },
        toggleSelection(rows) {
            if (rows) {
                rows.forEach(row => {
                    this.$refs.multipleTable.toggleSelection(row);
                });
            } else {
                this.$refs.multipleTable.clearSelection();
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        handleSizeChange(val) {
            this.pagesize = val;
            this.initExceptinolist();
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.initExceptinolist();
        }
    },
}